package com.example.zhangwei.widgts;

import com.example.zhangwei.R;
import com.example.zhangwei.ui.demo.ActivityLiving;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class NotificationWidget {
	
	private static NotificationWidget instance;
	
	private Context context;
	
	private NotificationManager notificationManager;
	
	private NotificationWidget(Context context) {
		// TODO Auto-generated method stub
		this.context = context;
		this.notificationManager = (NotificationManager) context.getSystemService(Application.NOTIFICATION_SERVICE);
	}
	
	public static synchronized NotificationWidget getInstance(Context context)
    {
		if (instance == null)
		{
			instance = new NotificationWidget(context);
		}
		return instance;
    }
	
	
	/**
	 * 系统通知栏
	 **/
	@SuppressWarnings("deprecation")
	public void setNotification(int id)
	{
		long when = System.currentTimeMillis();
		
		/* 创建Notication，并设置相关参数*/
		Notification notification = new Notification();
		/* 设置statusbar显示的icon */
		notification.icon = R.drawable.ic_launcher;
		/* 设置statusbar显示的时间  */
		notification.when = when;
		/* 设置statusbar显示的标题 */
		notification.tickerText = "通知";
		/* 设置notification发生时同时发出默认声音 *//* 设置notification震动 */
		notification.defaults |= Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;
		/* 在通知栏上点击此通知后自动清除此通知  */
		notification.flags = Notification.FLAG_AUTO_CANCEL;
				//|Notification.FLAG_ONGOING_EVENT //FLAG_ONGOING_EVENT将此通知放到通知栏的"Ongoing"即"正在运行"组中   
				//|Notification.FLAG_NO_CLEAR; //点击clear也不会被清除FLAG_NO_CLEAR
		/* 设置notification震动时间数组*/
		long[] vibreate= new long[]{2000,2000,2000,2000,2000};
		notification.vibrate = vibreate;
		
		CharSequence contentTitle = "通知标题"; //通知栏标题

		CharSequence contentText = "通知的详细内容!"; //通知栏内容

		Intent notificationIntent = new Intent(context,ActivityLiving.class); //点击该通知后要跳转的Activity

		PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0);//pendingintent 延期的意图 
		
		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
		
		notificationManager.notify(id, notification);
	}
	
	/** 
	 * 关闭系统通知栏
	 **/
	public void cancelNotification(int id)
	{
		if (id == -1)
		{
			notificationManager.cancelAll();
		}
		else
		{
			notificationManager.cancel(id);
		}
		
	}

}
